import {
  splitMenuLevel,
  findMenu,
  findShowMenu,
  findMenuFirstChild
} from '@/utils/menusUtil'
import Cookies from 'js-cookie'
const menus = {
  state: {
    sidebar: {
      opened: !+Cookies.get('sidebarStatus'),
      hid: false
    },
    menus: {},
    // menuActiveIndex:Cookies.get('menuActiveIndex'),
    sidebarMenus: [],
    sidebarMenusIndex: '',
    curMenu: null,
    codeMenu: null,
    homePageMenu: null,
    shopListMenuActiveShop: null
  },
  mutations: {
    SET_CURMENU: (state, curMenu) => {
      state.curMenu = curMenu
    },
    TOGGLE_SIDEBAR: (state) => {
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1)
      } else {
        Cookies.set('sidebarStatus', 0)
      }
      state.sidebar.opened = !state.sidebar.opened
    },
    CLOSE_SIDEBAR: (state) => {
      Cookies.set('sidebarStatus', 0)
      state.sidebar.opened = false
    },
    OPEN_SIDEBAR: (state) => {
      if (!+Cookies.get('sidebarStatus')) {
        state.sidebar.opened = true
      }
    },
    REFRESH_CUR_MENU: (state, router) => {
      if (Object.keys(router.meta).length === 0) {
        // 没有meta信息, 直接返回
        state.sidebar.hid = true
        return -1
      }

      const curMenu = splitMenuLevel(router.meta)
      state.sidebar.hid = !(curMenu.length >= 2)
      if (!state.curMenu || curMenu[0].code !== state.curMenu[0].code) {
        state.sidebarMenus = findMenu(state.menus, curMenu[0].code)
          ? findMenu(state.menus, curMenu[0].code).children
          : []
        state.sidebarMenusIndex = curMenu[curMenu.length - 1].code
      } else {
        state.sidebarMenusIndex = curMenu[curMenu.length - 1].code
      }
      state.curMenu = curMenu
    },

    RELOAD_MENUS: (state, menus) => {
      const showMenu = JSON.parse(JSON.stringify(menus))
      findShowMenu(showMenu)
      state.homePageMenu = findMenuFirstChild(showMenu)
      state.menus = showMenu
    }
  },

  actions: {
    setCurmenu({ commit }, curmenu) {
      console.log(curmenu)
      commit('SET_CURMENU', curmenu)
    },
    ToggleSideBar: ({ commit }) => {
      commit('TOGGLE_SIDEBAR')
    },
    CloseSideBar({ commit }) {
      commit('CLOSE_SIDEBAR')
    },
    OpenSideBar({ commit }) {
      commit('OPEN_SIDEBAR')
    },
    ToggleDevice({ commit }, device) {
      commit('TOGGLE_DEVICE', device)
    },
    ReloadMenus: ({ commit, rootGetters }, menus) => {
      if (!rootGetters.businessResourceBtns.includes('MEMBER_BIND_DEVICE')) {
        // 如果是云南卡卡相关需求，需要单独控制下路由
        if (menus.length > 0) {
          menus.forEach((element, index) => {
            if (
              element.code === 'payCenterDir' &&
              element.title === '交易中心'
            ) {
              element.children.splice(
                element.children.findIndex(
                  (item) => item.code === 'ThirdPartySwipe'
                ),
                1
              ) // 第三方刷卡交易
            }
            if (
              element.code === 'MarketingDir' &&
              element.title === '营销中心'
            ) {
              console.log('element', element)
              element?.children?.map((child) => {
                if (
                  child.code === 'MarketingActivityDir' &&
                  child.title === '营销活动'
                ) {
                  child.children.splice(
                    child.children.findIndex(
                      (item) => item.code === 'MarketingCouponTask'
                    ),
                    1
                  ) // 赠券任务
                }
              })
            }
          })
        }
      }
      commit('RELOAD_MENUS', menus, { root: true })
    },
    RefreshCurMenu: ({ commit }, router) => {
      commit('REFRESH_CUR_MENU', router)
    }
  }
}

export default menus
